package com.zzty.zzlp.controller;

import com.zzty.zzlp.commom.PropertyUtil;
import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("/ChangePassword")
public class PwdChangeController {

    @Autowired
    private JavaMailSenderImpl sender;

    @Autowired
    UserService userService;

    @RequestMapping("/emailPwdRepaire")
    @ResponseBody
    public Boolean emailPwdRepaire(@RequestBody User user){
        String pwdRepaire= UUID.randomUUID().toString().replace("-", "");
        user.setPwdRepaire(pwdRepaire);
        userService.emailPwdRepaire(user);
        return (sendMail(user.getEmail(),pwdRepaire));
    }

    public Boolean sendMail(String receiver,String pwdRepaire){
        MimeMessage mimeMessage = sender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            mimeMessageHelper.setTo(receiver);
            mimeMessageHelper.setFrom(PropertyUtil.prop.getProperty("spring.mail.username"));
            mimeMessageHelper.setSubject("自在天原");
            String content = "<html><body>" +"<h3>你好!<br>" +
                    "此邮件用于重置您的自在天原密码，<br>" +
                    "请点击以下链接找回密码：</h3><br>"+
                    "<h3><a href='http://www.icymi.cn/zzlp/ChangePassword/changePwdByEmail?pwdRepaire="+pwdRepaire+"'>http://www.icymi.cn/zzlp/Register/changePwdByEmail</a></h3>" +
                    "</body></html>";
            mimeMessageHelper.setText(content,true);
            sender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @RequestMapping("/changePwdByEmail")
    @ResponseBody
    public void changePwdByEmail(@Param("pwdRepaire") String pwdRepaire, HttpServletRequest request,HttpServletResponse response){
        User user = userService.searchUserByPwdRepaire(pwdRepaire);
        if(user != null){
            try {
                request.getSession().setAttribute("pwdRepaire",pwdRepaire);
                response.sendRedirect("/zzlp/html/mailPwdReset.html");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else{
            try {
                response.sendRedirect("/zzlp/html/abate.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @RequestMapping("/emailPwdReset")
    @ResponseBody
    public Boolean emailPwdReset(HttpServletRequest request,@RequestBody User user){
        String pwdRepaire = (String)request.getSession().getAttribute("pwdRepaire");
        user.setPwdRepaire(pwdRepaire);
        request.getSession().removeAttribute("pwdRepaire");
        if(userService.changePwdByMail(user)){
            userService.delPwdRepaire(userService.searchUserByPwdRepaire(pwdRepaire).getId());
            return true;
        }else{
            return false;
        }
    }

    @RequestMapping("/telPwdReset")
    @ResponseBody
    public Boolean telPwdReset(@RequestBody User user){
        if(userService.changePwdByTel(user)){
            return true;
        }else{
            return false;
        }
    }

}
